"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
/**
 * 节点遍历，使用 callback 进行处理
 * 如果 callback 返回 true，将会中止遍历
 */
const forEach = (treeNodes, callback, options) => {
  const {
    level = 0,
    childrenKey = 'children',
    parent,
    order = 'lrd'
  } = options ?? {};
  let breaked;
  for (const treeNode of treeNodes) {
    const index = treeNodes.indexOf(treeNode);
    const children = treeNode[childrenKey];
    let result;
    if (order == 'dlr') {
      result = callback(treeNode, {
        level,
        index,
        parent,
        isMaybeLeaf: !children?.length
      });
      if (result) {
        breaked = result;
        break;
      }
    }
    if (children?.length) {
      result = forEach(children, callback, {
        childrenKey,
        level: level + 1,
        parent: treeNode
      });
      if (result) {
        breaked = result;
        break;
      }
    }
    if (order == 'lrd') {
      result = callback(treeNode, {
        level,
        index,
        parent,
        isMaybeLeaf: !children?.length
      });
      if (result) {
        breaked = result;
        break;
      }
    }
  }
  return breaked;
};
var _default = exports.default = forEach;